package sps.admin.module.system.employee.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import sps.admin.constant.AdminSwaggerTagConst;
import sps.admin.module.business.workinghours.service.WorkingHoursService;
import sps.admin.module.system.department.domain.vo.DepartmentVO;
import sps.admin.module.system.employee.domain.entity.EmployeeEntity;
import sps.admin.module.system.employee.domain.form.*;
import sps.admin.module.system.employee.domain.form.*;
import sps.admin.module.system.employee.domain.vo.EmployeeVO;
import sps.admin.module.system.employee.service.EmployeeService;
import sps.base.common.domain.PageResult;
import sps.base.common.domain.ResponseDTO;
import sps.base.common.util.SmartRequestUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * 员工
 *
 * @Author 江苏圣普斯安防科技有限公司
 * @Date 2021-12-09 22:57:49
 * @Copyright  <a href="">江苏圣普斯安防科技有限公司</a>
 */
@RestController
@Tag(name = AdminSwaggerTagConst.System.SYSTEM_EMPLOYEE)
public class EmployeeController {

    @Resource
    private EmployeeService employeeService;

    @Resource
    private WorkingHoursService workingHoursService;

    @PostMapping("/employee/query")
    @Operation(summary = "员工管理查询 ")
    public ResponseDTO<PageResult<EmployeeVO>> query(@Valid @RequestBody EmployeeQueryForm query) {
        return employeeService.queryEmployee(query);
    }

    @Operation(summary = "添加员工(返回添加员工的密码) ")
    @PostMapping("/employee/add")
    @SaCheckPermission("system:employee:add")
    public ResponseDTO<String> addEmployee(@Valid @RequestBody EmployeeAddForm employeeAddForm) {
        return employeeService.addEmployee(employeeAddForm);
    }

    @Operation(summary = "更新员工 ")
    @PostMapping("/employee/update")
    @SaCheckPermission("system:employee:update")
    public ResponseDTO<String> updateEmployee(@Valid @RequestBody EmployeeUpdateForm employeeUpdateForm) {
        return employeeService.updateEmployee(employeeUpdateForm);
    }

    @Operation(summary = "更新登录人信息")
    @PostMapping("/employee/update/login")
    public ResponseDTO<String> updateByLogin(@Valid @RequestBody EmployeeUpdateForm employeeUpdateForm) {
        employeeUpdateForm.setEmployeeId(SmartRequestUtil.getRequestUserId());
        return employeeService.updateEmployee(employeeUpdateForm);
    }

    @Operation(summary = "更新登录人头像")
    @PostMapping("/employee/update/avatar")
    public ResponseDTO<String> updateAvatar(@Valid @RequestBody EmployeeUpdateAvatarForm employeeUpdateAvatarForm) {
        employeeUpdateAvatarForm.setEmployeeId(SmartRequestUtil.getRequestUserId());
        return employeeService.updateAvatar(employeeUpdateAvatarForm);
    }

    @Operation(summary = "更新员工禁用/启用状态 ")
    @GetMapping("/employee/update/disabled/{employeeId}")
    @SaCheckPermission("system:employee:disabled")
    public ResponseDTO<String> updateDisableFlag(@PathVariable Long employeeId) {
        return employeeService.updateDisableFlag(employeeId);
    }

    @Operation(summary = "批量删除员工 ")
    @PostMapping("/employee/update/batch/delete")
    @SaCheckPermission("system:employee:delete")
    public ResponseDTO<String> batchUpdateDeleteFlag(@RequestBody List<Long> employeeIdList) {
        return employeeService.batchUpdateDeleteFlag(employeeIdList);
    }

    @Operation(summary = "批量调整员工部门 ")
    @PostMapping("/employee/update/batch/department")
    @SaCheckPermission("system:employee:department:update")
    public ResponseDTO<String> batchUpdateDepartment(@Valid @RequestBody EmployeeBatchUpdateDepartmentForm batchUpdateDepartmentForm) {
        return employeeService.batchUpdateDepartment(batchUpdateDepartmentForm);
    }

    @Operation(summary = "修改密码 ")
    @PostMapping("/employee/update/password")
    public ResponseDTO<String> updatePassword(@Valid @RequestBody EmployeeUpdatePasswordForm updatePasswordForm) {
        updatePasswordForm.setEmployeeId(SmartRequestUtil.getRequestUserId());
        return employeeService.updatePassword(updatePasswordForm);
    }

    @Operation(summary = "重置员工密码 ")
    @GetMapping("/employee/update/password/reset/{employeeId}")
    @SaCheckPermission("system:employee:password:reset")
    public ResponseDTO<String> resetPassword(@PathVariable Integer employeeId) {
        return employeeService.resetPassword(employeeId);
    }

    @Operation(summary = "查询员工-根据部门id ")
    @GetMapping("/employee/queryEmployeeByDeptId/{departmentId}")
    public ResponseDTO<List<EmployeeVO>> queryEmployeeByDeptId(@PathVariable long departmentId) {
        return employeeService.getAllEmployeeByDepartmentId(departmentId, Boolean.FALSE);
    }

    @Operation(summary = "查询员工-根据部门id ")
    @GetMapping("/employee/queryEmployeeByDeptIds/{departmentIds}")
    public ResponseDTO<List<EmployeeVO>> queryEmployeeByDeptIds(@PathVariable String departmentIds) {
        String[] arrs = departmentIds.split(",");
        List<Long> ids = new ArrayList<>();
        for(int i = 0;i < arrs.length;i++){
            ids.add(Long.valueOf(arrs[i]));
        }
        return employeeService.getAllEmployeeByDepartmentIds(ids, Boolean.FALSE);
    }

    @Operation(summary = "查询所有员工 ")
    @GetMapping("/employee/queryAll")
    public ResponseDTO<List<EmployeeVO>> queryAllEmployee(@RequestParam(value = "disabledFlag", required = false) Boolean disabledFlag) {
        return employeeService.queryAllEmployee(disabledFlag);
    }

    @Operation(summary = "查询员工-根据员工工号 ")
    @GetMapping("/employee/queryByJonNumber/{jobNumber}")
    public ResponseDTO<EmployeeVO> queryEmployeeByJobNumber(@PathVariable String jobNumber) {
        return ResponseDTO.ok(employeeService.queryEmployeeByJobNumber(jobNumber));
    }

    @Operation(summary = "同步嘉宝人员数据")
    @GetMapping("/department/batchSyncEmployee")
    public ResponseDTO<DepartmentVO> batchSyncEmployee() {
        workingHoursService.batchSyncEmployee();
        return ResponseDTO.ok();
    }

    @Operation(summary = "更新员工隐藏/占状态 ")
    @GetMapping("/employee/update/hide/{employeeId}")
    @SaCheckPermission("system:employee:hide")
    public ResponseDTO<String> updateHideFlag(@PathVariable Long employeeId) {
        return employeeService.updateHideFlag(employeeId);
    }

    @Operation(summary = "更新员工大小周限制 ")
    @GetMapping("/employee/update/limit/{employeeId}")
    @SaCheckPermission("system:employee:limit")
    public ResponseDTO<String> updateLimitFlag(@PathVariable Long employeeId) {
        return employeeService.updateLimitFlag(employeeId);
    }

    @Operation(summary = "查询员工(判断上班是否大小周)-根据部门id")
    @GetMapping("/employee/selectEmployeeByDeptIds/{departmentIds}/{overtimeData}/{overtimeFlag}")
    public ResponseDTO<List<EmployeeVO>> selectEmployeeByDeptIds(@PathVariable String departmentIds,@PathVariable Boolean overtimeFlag,@PathVariable LocalDate overtimeData) {
        String[] arrs = departmentIds.split(",");
        List<Long> ids = new ArrayList<>();
        for(int i = 0;i < arrs.length;i++){
            ids.add(Long.valueOf(arrs[i]));
        }
        return employeeService.selectEmployeeByDepartmentIds(ids,overtimeData,overtimeFlag, Boolean.FALSE);
    }
}
